<?php
$merchants_lbl = BUtils::T('merchants_lbl');
$login_lbl = BUtils::T('login_lbl');
$follow_tips = '';//BUtils::T('follow_tips');

$this->breadcrumbs=array(
	$merchants_lbl => array('/' . $this->id),
	$row['name'],
);

$default_logo = Yii::app()->baseUrl . '/img/default_special_thumb.png';

//define array
$aWeekDays = BUtils::WeekDayKeys(7, true);
$a24Hours = BUtils::AllHour24();
$hours_list = MerchantObject::HtmlBusinessHours($aHours, $aWeekDays, $a24Hours);

$full_address = $row['address'] . ', ' . $row['postcode'] . ' ' . $row['city'];
$url_logo = BUtils::GetBusinessPictureUrl($row['url_logo']);
$brief = BUtils::GetText($row['brief']);

//get the latLng
if (empty($row['latitude']) or empty($row['longitude'])) {
	$latitude = 'null';
	$longitude = 'null';
	$address = "'{$full_address}'";
} else {
	$latitude = $row['latitude'];
	$longitude = $row['longitude'];
	$address = 'null';
}
?>
<div class="section-wrapper post-w">
<div class="container">
<div class="row">
	<div class="span8">
		<div class="white-card extra-padding relative">
			<div class="business-profile-info" style="position:relative;">
				<div class="business-logo pull-right">
					<img src="<?php echo $url_logo; ?>" alt="" onError="this.onerror=null;this.src='<?php echo $default_logo; ?>';" />
				</div>
				<div class="business-ul" style="overflow:hidden;">
					<h2 class="name"><?php echo $row['name']; ?></h2>
					<ul class="icons-ul">
						<li><i class="icon-li icon-map-marker"></i> <span class="street"><?php echo $row['address']; ?></span></li>
						<li><i class="icon-li icon-map-marker white"></i> <span class="city"><?php echo $row['postcode'] . ' ' . $row['city']; ?></span></li>
					</ul>
<?php if (!empty($row['phone'])) { ?>
					<ul class="icons-ul">
						<li><i class="icon-li icon-phone"></i> <span class="phone"><?php echo MerchantObject::formatPhoneNumber($row['phone']); ?></span></li>
					</ul>
<?php } ?>
					<?php echo $hours_list; ?>
				</div>
				<div class="actions">
					<?php
					if (!empty($row['url_site'])) echo CHtml::link('<i class="icon-globe"></i>', $row['url_site'], array('class'=>'btn btn-small', 'target'=>'_blank', 'rel'=>'nofollow')) . "\n";
					if (!empty($row['url_facebook_page'])) echo CHtml::link('<i class="icon-facebook-sign"></i>', $row['url_facebook_page'], array('class'=>'btn btn-small', 'target'=>'_blank', 'rel'=>'nofollow')) . "\n";
					if (Yii::app()->user->isGuest) {
						$btn_name = BUtils::T('follow_btn');
						$link = Yii::app()->createUrl('/accounts/login');
						$class = 'btn btn-small btn-success login-popup';
						//TODO: the following link show only for signed user
						//echo CHtml::link('<i class="icon-heart"></i> ' . $btn_name, $link, array('class'=>$class));
					} else {
						//start to make following button
						if (isset($status['follow']) and $status['follow'] == true) {
							$btn_name = BUtils::T('follow_cancel_btn');
							$link = Yii::app()->createUrl('/place/follow', array('id'=>$row['id']));
							$class = 'btn btn-small btn-success follow-place active';
						} else {
							$btn_name = BUtils::T('follow_btn');
							$link = Yii::app()->createUrl('/place/follow', array('id'=>$row['id']));
							$class = 'btn btn-small btn-success follow-place';
						}
						echo CHtml::link('<i class="icon-heart"></i> ' . $btn_name, $link, array('class'=>$class));
					}
					?>
				</div>
			</div>
		</div>
		<div class="row-fluid" style="margin-bottom:20px;">
			<div class="span12">
			<ul class="nav nav-tabs">
<?php if ($row['rewards'] > 0) { ?>
					<li><a data-toggle="tab" href="#tab-rewards" data-key="rewards" class="color-reward"><i class="icon-trophy"></i> <?php echo BUtils::T('rewards_lbl'); ?></a></li>
<?php } if ($row['offers'] > 0) { ?>
					<li><a data-toggle="tab" href="#tab-offers" data-key="offers" class="color-offer"><i class="icon-certificate"></i> <?php echo BUtils::T('offers_lbl'); ?></a></li>
<?php } if ($row['gallery'] > 0) { ?>
					<li><a data-toggle="tab" href="#tab-gallery" data-key="gallery" class="color-gallery"><i class="icon-picture"></i> <?php echo BUtils::T('gallery_lbl'); ?></a></li>
<?php } if (!empty($brief)) { ?>
					<li><a data-toggle="tab" href="#tab-brief" class="color-info hidden-tablet"><i class="icon-bookmark"></i> <?php echo BUtils::T('merchant_brief_lbl'); ?></a></li>
<?php } ?>
				</ul>
				<div class="tab-content">
<?php if ($row['rewards'] > 0) { ?>
					<div id="tab-rewards" class="tab-pane"></div>
<?php } if ($row['offers'] > 0) { ?>
					<div id="tab-offers" class="tab-pane"></div>
<?php } if ($row['gallery'] > 0) { ?>
					<div id="tab-gallery" class="tab-pane"></div>
<?php } if (!empty($brief)) { ?>
					<div id="tab-brief" class="tab-pane hidden-tablet"><p><?php echo $brief; ?></p></div>
<?php } ?>
				</div>
			</div>
		</div>
	</div>

	<!-- right side -->
	<div class="span4">
		<div class="white-card no-padding">
			<div id="venueMap" style="height:240px;" data-latitude="<?php echo $latitude; ?>" data-longitude="<?php echo $longitude; ?>"></div>
		</div>
		
<?php if (Yii::app()->user->isGuest and $row['enable_rewards'] == 1) { ?>
		<div class="white-card">
			<div style="text-align:center;">
				<h4 class="blue"><?php echo Yii::t('app', 'Login to check points'); ?></h4>
			</div>
			<hr />
			<div class="text-center">
				<a href="javascript:;" class="facebookButton btn-facebook"><?php echo Yii::t('app', 'Login with Facebook'); ?></a>
			</div>
			<div class="text-right">
				<a style="font-size:13px;text-decoration:none;" href="javascript:;" id="login-btn-old">
					<i class="icon-angle-right"></i>
					<?php echo Yii::t('app', 'Login with the old way'); ?>
				</a>
				<div class="login-form row-fluid text-left" style="display:none;" id="login-box-old">
					<div style="padding:0 24px;">
<?php
/** @var BootActiveForm $form */
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
	'id'=>'login-form',
	'action'=>Yii::app()->createUrl('/accounts/login'),
	'htmlOptions'=>array('class'=>''),
	'enableClientValidation'=>true,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
	),
));
echo $form->textFieldRow($model, 'user_input', array('class'=>'span12', 'placeholder'=>Yii::t('app','Enter your phone number or email') ));
echo $form->passwordFieldRow($model, 'password', array('class'=>'span12' ));
echo '<br /><div class="pull-left">';
echo CHtml::submitButton($login_lbl, array('class'=>'btn btn-primary'));
echo '</div><div class="pull-right">';
echo CHtml::link(Yii::t('app', 'Create a new account'), Yii::app()->createUrl('/accounts/signup'), array('class'=>'btn btn-link'));
echo '</div><div class=clearfix></div>';
$this->endWidget();
$form->errorSummary($model);
?>
					</div>
				</div>
			</div>
		</div>
<?php } elseif (!Yii::app()->user->isGuest and $row['enable_rewards'] == 1) { ?>
		<div class="white-card no-padding">
			<div class="profile-box text-center" style="margin:15px 0;">
				<a href="<?php echo Yii::app()->createUrl('/user'); ?>">
					<img style="width:64px;" src="<?php echo Yii::app()->user->picture; ?>" />
				</a>
			</div>
			<div class="userprofile">
				<table style="width:100%;">
					<tr>
						<td class="stat" style="width:33%;vertical-align:top;">
							<div class="num" id="current_pts">0</div>
							<div class="num-des">Current<br />points</div>
						</td>
						<td class="stat" style="vertical-align:top;">
							<div class="num" id="spend_pts">0</div>
							<div class="num-des">Redeemed<br />points</div>
						</td>
						<td class="stat" style="width:33%;vertical-align:top;">
							<div class="num" id="visits_cnt">0</div>
							<div class="num-des">Visits</div>
						</td>
					</tr>
				</table>
			</div>
		</div>
<?php } ?>
		
		<div class="white-card">
			<div style="text-align:center;">
				<h4 class="blue"><?php echo BUtils::T('get_free_app_lbl') ?></h4>
			</div>
			<div style="text-align:center;">
				<a href="<?php echo Yii::app()->createUrl('app/mobile', array('download'=>1, 'type'=>'iphone', 'ref'=>$row['id'])); ?>" target="_blank"><img src="<?php echo Yii::app()->baseUrl; ?>/img/app-store-icon.png" alg="Yamii in App Store" style="width:100px;" /></a>
				<a href="<?php echo Yii::app()->createUrl('app/mobile', array('download'=>1, 'type'=>'android', 'ref'=>$row['id'])); ?>" target="_blank"><img src="<?php echo Yii::app()->baseUrl; ?>/img/google-play-icon.png" alg="Yamii in Google Play Store" style="width:100px;" /></a>
			</div>
		</div>
		
<?php
//tags
$tags_html = '';
$keys = BUtils::DetailFeaturesKeys();
foreach ($keys as $key) {
	if (!empty($row[$key])) {
		$string = '';
		$array = explode(',', $row[$key]);
		foreach ($array as $item) {
			$string .= '<span class="label tags">' . BUtils::T($item) . '</span> ';
		}
		$tags_html .= '<div class="tags-title">' . BUtils::T($key . '_lbl') . ':</div><div class="tags-body">'.$string.'</div>';
	}
}
if (!empty($tags_html)) {
?>
<div class="white-card">
	<div class="title-bar">
		<h4 class="blue"><?php echo BUtils::T('detail_features_lbl'); ?></h4>
	</div>
	<div class="panel panel-with-title">
		<div class="dishItems clearfix">
			<?php echo $tags_html; ?>
		</div>
	</div>
</div>
<?php } ?>
		
	</div><!-- end of span4 -->
</div>
</div>
</div>
<?php
$langcode = Yii::app()->language;
if ($langcode == 'zh_cn') $langcode = 'en';
?>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places&amp;language=<?php echo $langcode; ?>"></script>
<script src="<?php echo Yii::app()->baseUrl; ?>/js/jquery.google.map.js?v=<?php echo Yii::app()->params['version']; ?>" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
	//popup special box and gallery
	$('.login-popup').colorbox({rel:'dish',transition:'none'});
	$('.gallery-space a.thumb').colorbox({rel:'thumb',transition:'none'});
	$('.btn-facebook').click(function(){
		fbUI.login('<?php echo Yii::app()->createUrl('/accounts/facebook'); ?>');
	});
	
	//init the map
	var mapCav = $("#venueMap").isMapCanvas('<?php echo Yii::app()->baseUrl; ?>', 0);
	mapCav.pin('mid_<?php echo $row['id']; ?>', {
		style: 'blue',
		category: '<?php echo $row['category_key']; ?>',
		latitude: <?php echo $latitude; ?>,
		longitude: <?php echo $longitude; ?>
	});
	
	//tab ajax loading
	var cache = [];
	$('a[data-toggle="tab"]').on('shown', function (e) {
		var key = $(this).data('key'), url = '';
		if (key == '') return;
		if (key == 'rewards') {
			url = '<?php echo Yii::app()->createUrl('/place/rewards', array('id'=>$row['id'])); ?>';
		} else if (key == 'offers') {
			url = '<?php echo Yii::app()->createUrl('/place/offers', array('id'=>$row['id'])); ?>';
		} else if (key == 'gallery') {
			url = '<?php echo Yii::app()->createUrl('/place/gallery', array('id'=>$row['id'])); ?>';
		}
		
		var _panel = $('#tab-'+key);
		if (url != '') {
			if (!cache[key]) {
				$.ajax({
					url: url, data: {t:null}, type: 'POST',
					beforeSend: function() {
						_panel.html('<div class="text-center"><img src="<?php echo Yii::app()->baseUrl; ?>/img/loader.gif" /></div>');
					},
					error: function(jqXHR) {
						_panel.html(jqXHR.responseText);
					},
					success: function(html) {
						cache[key] = html;
						_panel.html(html);
					}
				});
			}
		}
    });
	//show the first tab
	$('a[data-toggle="tab"]:first').tab('show');
	
	//login box
	$('#login-btn-old').click(function(){
		$('#login-btn-old i').toggleClass('icon-angle-down');
		$('#login-box-old').slideToggle();
	});
	
	//loading user points
<?php if (!Yii::app()->user->isGuest and $row['enable_rewards'] == 1) { ?>
	$.get('<?php echo Yii::app()->createUrl('/user/points'); ?>', {mid:<?php echo $row['id']; ?>}, function(data){
		if (data.code === 200) {
			$('#current_pts').html(data.message.current_pts);
			$('#spend_pts').html(data.message.spend_pts);
			$('#visits_cnt').html(data.message.visits);
			Rank.init(data.message.current_pts, '#current_pts');
		}
	}, 'json');
<?php } ?>
});

var Rank = {
	// 初始化
	init: function(final, eId) {
		var rank_num = final;
		if (rank_num < 100) {
			return;
		}
		if (typeof Number(rank_num) === "number") {
			this._compute(rank_num, eId);
		}
	},
	// 按照一定规则计算
	_compute: function(rank_num, eId){
		var timer, temp_num, step, rate;
		// 第一次计算出一个初始值
		temp_num = 1;
		// 每次增加数值
		step = Math.round(Math.round(rank_num/2))/100;
		// 循环更新temp_num的值
		function _count(temp_num, step){
			temp_num = Math.round(temp_num + step * Math.random());
			// 调用 _show 方法
			Rank._show(temp_num, eId);
			// 根据条件判断，改变方法执行的时间间隔
			rate = (temp_num / rank_num > 0.95) ? 20 : 1;
			timer = setTimeout(function(){
				_count(temp_num, step);
			}, rate);
			if (temp_num > rank_num) {
				clearTimeout(timer);
				Rank._show(rank_num, eId);
			}
		}
		_count(temp_num, step);
	},
	// 显示数字的变化情况
	_show: function(temp_num, eId) {
		// 数字的变化
		$(eId).html(temp_num);
	}
};
</script>